#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   model_task_utils.py    
@Contact :   pengwei.sun@aihuishou.com
@License :   (C)Copyright aihuishou

@Modify Time      @Author       @Version    @Desciption
------------      -----------   --------    -----------
2020-01-07 16:48   pengwei.sun      1.0         None
'''
from src.utils.db_processor import mysql_prediction_processor,mysql_price_model
from src.utils.config import config, logger


insert_sql = """
        INSERT INTO sku_level_train_model_task_daily(category_id, task_date)
        VALUES({}, '{}')
        """
def insertDataToTaskTB(category_id, task_date):
    insertsql = insert_sql.format(category_id, task_date)
    mysql_prediction_processor.execute_sql(insertsql)
    logger.info('skulevel insert into data category_id={},task_date={}'.format(category_id, task_date))

def updateDataToTaskTB(is_success,category_id, task_date):

    update_success_sql = "UPDATE sku_level_train_model_task_daily SET is_success = {} WHERE category_id ={} and task_date='{}'".format(is_success,category_id,task_date)

    mysql_prediction_processor.execute_sql(update_success_sql)
    logger.info('skulevel update into data category_id={},date(task_date) ={}'.format(category_id, task_date))

def countData(category_id, task_date):
    countsql="select count(*) from sku_level_train_model_task_daily  WHERE category_id ={} and task_date='{}'".format(category_id,task_date)

    dfCount =  mysql_prediction_processor.load_sql(countsql)
    return dfCount.iloc[0, 0]


def deleteData(category_id, task_date):
    update_success_sql = "delete from sku_level_train_model_task_daily WHERE category_id ={} and task_date='{}'".format(category_id, task_date)

    mysql_prediction_processor.execute_sql(update_success_sql)
    logger.info('goodsCode delete into data date(task_date) ={}'.format(task_date))


if __name__ == '__main__':
    print(countData(1, '2020-01-07'))
    deleteData('2020-01-07')
    insertDataToTaskTB(1,'2020-01-07')
    updateDataToTaskTB(1,'2020-01-07')
    dfCount=countData(1,'2020-01-07')
    print(dfCount)
    deleteData(1,'2020-01-07')
